var basePath = $("#basePath").val();
$(document).ready(function (){
    menuCss("合同管理");

    $("#queryForm").find("input[name='startDate']").datetimepicker({
        autoclose: true,
        todayHighlight: true,
        language: 'zh-CN',
        forceParse: true,
        format: 'yyyy-mm-dd',
        minView: 2,
        todayBtn: true
    });
    $("#queryForm").find("input[name='endDate']").datetimepicker({
        autoclose: true,
        todayHighlight: true,
        language: 'zh-CN',
        forceParse: true,
        format: 'yyyy-mm-dd',
        minView: 2,
        todayBtn: true
    });
    queryContractList(0,10);
    $("#show-count").change(function(){
        queryContractList(0,$("#show-count").val());
    })
    $("#search").click(function () {
        queryContractList(0,10);
    });

    $("#contractExecl").click(function (){
        location.href=basePath+"/contract/contractExecl";
    });

    $("#estimateExecl").click(function (){
        location.href=basePath+"/contract/estimateExecl";
    });

    $("#upload").click(function (){
        uploadDevice();
    });
});


function queryContractList(start,limit){
        var json = {};
        json.start = start;
        json.end = $("#show-count").val();
        var allName = $("#queryForm").find("[name='allName']").val();
        var firstPartyName = $("#queryForm").find("[name='firstPartyName']").val();
        var secondPartyName = $("#queryForm").find("[name='secondPartyName']").val();
        var startDate = $("#queryForm").find("[name='startDate']").val();
        var endDate = $("#queryForm").find("[name='endDate']").val();
        json.allName = allName;
        json.firstPartyName = firstPartyName;
        json.secondPartyName = secondPartyName;
        json.startDate = startDate;
        json.endDate = endDate;
        var url = basePath + "/contract/contractList";
        publicAjax(url,"POST",json,function (data){
            bindContractListData(data.data.list);
            $("#total-count").text(data.data.count);
        });
}


function bindContractListData(obj){
    $("#contract_table").html("");
    var html = "";
    if(obj == null || obj.length == 0){
        html="<tr><td class='text-center' colspan='12'>暂无数据</td></tr>";
    }

    $.each(obj,function (index,item) {
        html+="<tr><td class='text-center text-muted'>"+(index+1)+"</td>" +
            "<td class='text-center text-muted'><input type='checkbox' class='flat-green' name='one' value='"+item.contractId+"'></td>" +
            "<td class='text-center text-muted'><button type='button' class='btn btn-primary btn-xs' onclick='queryContractInfo("+item.contractId+")' data-toggle='tooltip' title='详情'><i class='fa fa-file-text-o'></i></button></td>" +
            "<td class='text-muted'>"+(item.contractNo==null?'':item.contractNo)+"</td>" +
            "<td class='text-muted'>"+(item.contractName==null?'':item.contractName)+"</td>" +
            "<td class='text-muted'>"+(item.hospitalName==null?'':item.hospitalName)+"</td>" +
            "<td class='text-muted'>"+(item.supplierName==null?'':item.supplierName)+"</td>" +
            "<td class='text-muted'>"+(item.signDate==null?'':item.signDate)+"</td>" +
            "</tr>"
    });
    $("#contract_table").html(html);
    initDomCss();
}

function initDomCss(){
    $('input[type="checkbox"].flat-green').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });

    $("input[name='one']").on('ifChecked', function(event){
        var current = $(this).parents("tr").index();
        $("[name='one']:checked").each(function(i){
            if($(this).parents("tr").index()!=current){
                $(this).iCheck('uncheck');
            }
        });
    });
}

function queryContractInfo(id){
    var url = basePath + "/contract/info?id="+id;
    location.href=url;
}

function uploadDevice() {
    if(!$("input[name='one']:checked").length){
        flavrShowByTime("请选择一个合同", null, "danger", false);
        return;
    }
    var keyId = $("input[name='one']:checked").val();
    var keyType = 1400;
    var root = basePath;
    var fileType = "contract_type";
    var json ={keyId:keyId,keyType:keyType,root:root,fileType:fileType,type:fileType};

    $("#contractFileDiv").fileUpload(
        json
    )
}

//分页
function Pagination(total,limit,current){
    $('.M-box3').empty();
    $('.M-box3').pagination({
        totalData:total,
        showData:limit,//数据总数totalData和每页显示的条数showData必须同时配置，否则默认使用总页数pageCount;limit可选值 5 10 20 50
        //pageCount:50,
        current:current,
        jump:true,
        coping:true,
        homePage:'首页',
        endPage:'末页',
        prevContent:'上页',
        nextContent:'下页',
        callback:function(index){
          start = (index.getCurrent()-1)*limit;
          limit = index.getShowData();
          queryContractList(start,limit);
        }
  });
}